package com.jyw.business.ins.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jyw.business.ins.domain.InsTask;
import com.jyw.business.ins.domain.InsTaskRectify;
import com.jyw.business.ins.domain.InsTransmitRecord;
import com.jyw.business.ins.domain.dto.web.InsTransmitRecordAddDto;
import com.jyw.business.ins.domain.vo.web.InsTransmitRecordVO;
import com.jyw.business.ins.enums.TransmitTypeEnum;
import com.jyw.business.ins.mapper.InsTransmitRecordMapper;
import com.jyw.business.ins.service.IInsTaskRectifyService;
import com.jyw.business.ins.service.IInsTaskService;
import com.jyw.business.ins.service.IInsTransmitRecordService;
import com.jyw.common.utils.bean.BeanUtils;
import com.jyw.system.utils.ConversionUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 巡检转交记录Service业务层处理
 *
 * @author zhangcheng
 * @date 2023-10-09
 */
@Service
public class InsTransmitRecordServiceImpl extends ServiceImpl<InsTransmitRecordMapper, InsTransmitRecord> implements IInsTransmitRecordService {
    @Resource
    private InsTransmitRecordMapper insTransmitRecordMapper;

    @Resource
    private IInsTaskService insTaskService;

    @Resource
    private IInsTaskRectifyService rectifyService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void transmitRecord(InsTransmitRecordAddDto insTransmitRecordAddDto) {
        Long taskId=insTransmitRecordAddDto.getTaskId();
        Long receiveUserId=insTransmitRecordAddDto.getReceiveUserId();
        InsTask insTask=insTaskService.selectInsTaskById(taskId);
        if(insTask!=null){
            Long oldInspectUserId=insTask.getInspectUserId();
            InsTransmitRecord insTransmitRecord=new InsTransmitRecord();
            insTransmitRecord.setBusinessId(taskId);
            insTransmitRecord.setTransmitType(TransmitTypeEnum.INSPECTION_TASK.getValue());
            insTransmitRecord.setTransmitUserId(oldInspectUserId);
            insTransmitRecord.setReceiveUserId(receiveUserId);
            insTransmitRecord.setCreateTime(LocalDateTime.now());
            insTransmitRecordMapper.insert(insTransmitRecord);
            //替换巡检人
            insTask.setInspectUserId(receiveUserId);
            insTaskService.updateById(insTask);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void transmitRecord(Long businessId, Long receiveUserId) {
        InsTaskRectify rectify = rectifyService.getById(businessId);
        if(!ObjectUtils.isEmpty(rectify)){
            Long oldRectifyUserId=rectify.getRectifyUserId();
            InsTransmitRecord insTransmitRecord=new InsTransmitRecord();
            insTransmitRecord.setBusinessId(businessId);
            insTransmitRecord.setTransmitType(TransmitTypeEnum.INSPECTION_RECTIFY.getValue());
            insTransmitRecord.setTransmitUserId(oldRectifyUserId);
            insTransmitRecord.setReceiveUserId(receiveUserId);
            insTransmitRecord.setCreateTime(LocalDateTime.now());
            insTransmitRecordMapper.insert(insTransmitRecord);
        }
    }

    @Override
    public List<InsTransmitRecordVO> queryListByTaskId(Long taskId) {
        QueryWrapper<InsTransmitRecord> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(InsTransmitRecord::getBusinessId,taskId);
        queryWrapper.lambda().eq(InsTransmitRecord::getTransmitType,TransmitTypeEnum.INSPECTION_TASK.getValue());
        //排序
        queryWrapper.lambda().orderByDesc(InsTransmitRecord::getCreateTime);
        List<InsTransmitRecord> list=insTransmitRecordMapper.selectList(queryWrapper);
        if(CollectionUtil.isNotEmpty(list)){
            List<InsTransmitRecordVO> newList=new ArrayList<>();
            list.forEach(record->{
                InsTransmitRecordVO info=new InsTransmitRecordVO();
                BeanUtils.copyProperties(record,info);
                newList.add(info);
            });
            return ConversionUtil.conversionList(newList,InsTransmitRecordVO.class);
        }
        return null;
    }

    @Override
    public List<InsTransmitRecordVO> queryListByRectifyId(Long id) {
        QueryWrapper<InsTransmitRecord> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(InsTransmitRecord::getBusinessId,id);
        queryWrapper.lambda().eq(InsTransmitRecord::getTransmitType,TransmitTypeEnum.INSPECTION_RECTIFY.getValue());
        //排序
        queryWrapper.lambda().orderByDesc(InsTransmitRecord::getCreateTime);
        List<InsTransmitRecord> list=insTransmitRecordMapper.selectList(queryWrapper);
        if(CollectionUtil.isNotEmpty(list)){
            List<InsTransmitRecordVO> collect = list.stream().map(record -> {
                InsTransmitRecordVO info = new InsTransmitRecordVO();
                BeanUtils.copyProperties(record, info);
                return info;
            }).collect(Collectors.toList());
            return ConversionUtil.conversionList(collect,InsTransmitRecordVO.class);
        }
        return null;
    }
}
